/*
   Copyright 2009 Marcin Szczepanski, webqem (marcins@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package webqem.wave.gadget.napkin
{
	

	public class NapkinState
	{
		public static const VERSION:int = 2;
		
        public var version:int;
        public var ownerId:String;
        public var commands:Array /* of NapkinCommand */
        public var timestamp:Number;

        public var isUndone:Boolean;


        public function addCommand(commandType:String, parameters:Array = null):void
        {
        	var command:NapkinCommand = new NapkinCommand(commandType, parameters);
        	commands.push(command);
        }

        public function fromString(value:String):void
        {
            var values:Array = value.split("|"); // resplit differently
            var versionPart:String = String(values[0]);
            
            version = 0;

            if(versionPart.charAt(0) == "v") { // new style                
                version = int(versionPart.substr(1));
            } else {
                throw new Error("Invalid version", versionPart);	
            }
                
            var command:NapkinCommand;
            var i:uint = 0;
        	
        	// timestamp is second value after version in v2+
        	this.timestamp = values[1];
                    
            for each(var value:String in values) {
                var commandCode:String = value.charAt(0);
                var parameter:String = value.substr(1);
                var pieces:Array = parameter.split(",");
                
                command = new NapkinCommand(commandCode, pieces);
                commands.push(command);
                if(command.type == NapkinCommand.UNDO) {
                	this.isUndone = true;
                }
            }   
        }

       public function belongsTo(id:String):Boolean
       {
       	return (ownerId == id);
       }

       public function containsCommandType(type:String):Boolean
       {
        for each(var command:NapkinCommand in commands) {
        	if(command.type == type) {
        		return true;
        	}
        }
        return false;
       }

       public function toString():String
       {
       	    var stateString:String = "";
       	    var stateParts:Array = new Array();
            
            stateParts.push("v" + this.version);
            stateParts.push(this.timestamp);
            for each(var command:NapkinCommand in commands) {
            	stateParts.push(command.toString());
            }
            return stateParts.join("|");
        }

		public function NapkinState()
		{
			this.commands = new Array();
			this.version = VERSION;
			this.timestamp = new Date().time;
		}
	}
}